from rest_framework import status
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from .models import Course
from .serializers import CourseSerializer
from rest_framework.response import Response

class CourseAPIView(APIView):
    def get(self,request):
        instance = Course.objects.all()
        serializer = CourseSerializer(instance,many=True)
        return Response(serializer.data)

    def post(self,request):

        data = request.data

        serializer = CourseSerializer(data=data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)

class CourseAPIView2(APIView):

    def get(self,request,pk):
        try:
            instance = Course.objects.get(id=pk)
        except:
            return Response(status=status.HTTP_404_NOT_FOUND)
        serializer = CourseSerializer(instance)
        return Response(serializer.data)

    def put(self,request,pk):
        try:
            instance = Course.objects.get(id=pk)
        except:
            return Response(status=status.HTTP_404_NOT_FOUND)
        data = request.data
        serializer = CourseSerializer(instance, data=data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)

    def delete(self,request,pk):
        try:
            Course.objects.get(id=pk).delete()
        except:
            pass
        return Response(status= status.HTTP_204_NO_CONTENT)


from rest_framework.generics import GenericAPIView

class CourseGenericAPIView(GenericAPIView):
    queryset = Course.objects.all()
    serializer_class = CourseSerializer

    def get(self,request):
        instance = self.get_queryset()
        serializer = self.get_serializer(instance,many=True)
        return Response(serializer.data)

    def post(self,request):
        data = request.data
        serializer = self.get_serializer(data=data)
        serializer.is_valid(raise_exception= True)
        serializer.save()
        return Response(serializer.data)

class CourseGenericAPIView2(GenericAPIView):
    queryset = Course.objects.all()
    serializer_class = CourseSerializer
    def get(self,request,pk):
        instance = self.get_object()
        serializer = self.get_serializer(instance)
        return Response(serializer.data)

    def put(self,request,pk):
        data = request.data
        instance = self.get_object()
        serializer = self.get_serializer(instance,data=data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)

    def delete(self,request,pk):
        try:
            self.get_object()
        except:
            pass
        return Response(status=status.HTTP_204_NO_CONTENT)


from rest_framework.mixins import ListModelMixin,CreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin

class CourseMixinsView(GenericAPIView,ListModelMixin,CreateModelMixin):
    queryset = Course.objects.all()
    serializer_class = CourseSerializer
    def get(self,request):
        return self.list(request)

    def post(self,request):
        return self.create(request)

class CourseMixinsView2(GenericAPIView,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin):
    queryset = Course.objects.all()
    serializer_class = CourseSerializer

    def get(self,request,pk):
        return self.retrieve(request,pk)

    def put(self,request,pk):
        return self.update(request,pk)

    def delete(self,request,pk):
        return self.destroy(request,pk)